import json

from django.db import models
from django.contrib.auth.models import AbstractUser
from utils.models import BaseModel
from settings import const_settings


class User(AbstractUser):
    tel = models.CharField(
        verbose_name='用户手机号',
        max_length=11,
        unique=True,
        null=True,
        blank=True,
    )
    email = models.CharField(
        verbose_name='用户邮箱',
        max_length=32,
        unique=True,
        null=True,
        blank=True
    )
    user_info = models.CharField(
        verbose_name='用户信息',
        max_length=32,
        default='0$1$0'
    )

    @property
    def is_complete(self):
        return self.user_info.split('$')[0]

    @property
    def user_or_seller(self):
        return self.user_info.split('$')[1]

    @property
    def info_id_or_stall_id(self):
        return int(self.user_info.split('$')[2])

    def __str__(self):
        if self.is_superuser:
            ident = '管理员'
        elif self.user_or_seller == '1':
            ident = '食客'
        else:
            ident = '摊主'
        return '<{}>'.format(ident) + self.username

    class Meta:
        verbose_name_plural = '用户表'


class Info(BaseModel):
    user_id = models.IntegerField(
        verbose_name='用户编号',
        default=0,
    )
    nickname = models.CharField(
        verbose_name='食客昵称',
        max_length=16,
        default='迷之食者',
    )
    avatar = models.ImageField(
        verbose_name='食客头像',
        upload_to='pic/avatar/',
        default='/pic/avatar/default_avatar.png',
    )

    @property
    def avatar_url(self):
        return const_settings.DRF_URL + 'media' + str(self.avatar)

    camp = models.IntegerField(
        verbose_name='食客阵营',
        choices=(
            (1, '饱餐'),
            (2, '品尝'),
            (3, '均衡')
        ),
        default=3
    )
    exp = models.IntegerField(
        verbose_name='食客经验值',
        default=0
    )

    @property
    def level(self):
        ret = self.exp // 100
        ret = 10 if ret > 10 else ret
        return ret

    def current_exp(self):
        exp = self.exp % 100
        if self.exp > 999:
            exp = 100
        return json.dumps({'height': '20px', 'background': 'coral', 'width': '{}%'.format(str(exp))})

    badge_num = models.CharField(
        verbose_name='食客所持徽章数量',
        max_length=32,
        default='1$1$1$1'
    )

    @property
    def badge_1_num(self):
        return int(self.badge_num.split('$')[0])

    @property
    def badge_2_num(self):
        return int(self.badge_num.split('$')[1])

    @property
    def badge_3_num(self):
        return int(self.badge_num.split('$')[2])

    @property
    def badge_4_num(self):
        return int(self.badge_num.split('$')[3])

    def __str__(self):
        return '<食客信息>{}'.format(self.nickname)

    class Meta:
        verbose_name_plural = '食客信息表'


class Coupon(BaseModel):
    user_id = models.IntegerField(
        verbose_name='满减券所属用户编号',
    )
    total_price = models.IntegerField(
        verbose_name='满减券总金额',
    )
    cut_price = models.IntegerField(
        verbose_name='满减券减少的金额',
    )

    def __str__(self):
        return '满<{}>减<{}>'.format(self.total_price, self.cut_price)

    class Meta:
        verbose_name_plural = '满减券表'


class CodeImg(BaseModel):
    is_use = models.BooleanField(
        verbose_name='是否启用',
        default=True,
    )
    cht = models.CharField(
        verbose_name='图片人物',
        max_length=16,
    )
    code_img = models.ImageField(
        verbose_name='验证码图片',
        upload_to='pic/code_img/'
    )

    @property
    def code_img_url(self):
        return const_settings.DRF_URL + 'media/' + str(self.code_img)

    is_most_handsome = models.BooleanField(
        verbose_name='是否最帅',
        default=False
    )
    is_most_old = models.BooleanField(
        verbose_name='是否最老',
        default=False
    )
    is_most_fat = models.BooleanField(
        verbose_name='是否最胖',
        default=False
    )
    is_pw = models.BooleanField(
        verbose_name='是否是虹桥炮王',
        default=False
    )
    is_most_fast = models.BooleanField(
        verbose_name='是否最快',
        default=False
    )
    is_tsl = models.BooleanField(
        verbose_name='是否有特斯拉',
        default=False
    )
    is_not_chinese = models.BooleanField(
        verbose_name='是否非中国籍',
        default=False
    )
    is_different = models.BooleanField(
        verbose_name='是否性别不同',
        default=False
    )

    def __str__(self):
        return '<{}>验证码图片'.format(self.cht)

    class Meta:
        verbose_name_plural = '验证码图片表'


class Track(BaseModel):
    user_id = models.IntegerField(
        verbose_name='用户编号',
    )
    stall_name = models.CharField(
        verbose_name='摊位名称',
        max_length=16
    )
    visit_time = models.CharField(
        verbose_name='访问时间',
        max_length=32
    )

    class Meta:
        verbose_name_plural = '足迹表'
